package com.example.demo.mongodb.controller;

import com.example.demo.entiy.Message;
import com.example.demo.entiy.User;
import com.example.demo.mongodb.repository.UserRepository;
import com.example.demo.mongodb.service.MessageService;
import com.example.demo.mongodb.service.MongoUserService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class MyTestController {
    @Resource
    private MongoUserService mongoUserService;

    @Resource
    private UserRepository userRepository;

    @Resource
    private MessageService messageService;

    @RequestMapping(value = "/saveUser")
    public String save(User user){
        mongoUserService.save(user);
        return user.toString();
    }

    @RequestMapping(value = "/findUserByName")
    public User findByName(@RequestParam String name){
        return mongoUserService.findByName(name);
    }

    @RequestMapping(value = "/findUserAll")
    public List<User> findAll(){
        List<User> userList = mongoUserService.findAll();

        return userList;
    }

    @RequestMapping(value = "/deleteUserById")
    public void deleteUserById(Long id){
        mongoUserService.deleteById(id);
    }

    @RequestMapping(value = "/saveMessage")
    public String saveMessage(Message message){
        messageService.insert(message);
        return message.toString();
    }

    @RequestMapping(value = "/findMessageById")
    public Message findById(Long id){
        return messageService.findById(id);
    }

}
